package com.hsj.goldstore.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.os.Build;
import android.support.annotation.RequiresApi;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.hsj.goldstore.R;

import java.util.List;


/**
 * Created by Administrator on 2018/4/2.
 */

public class RecycleViewItemderoctor extends RecyclerView.ItemDecoration {

    private static final int mHeigh = 100;
    private Paint mPaint;
//    private Bitmap bitmap;
    private Context context;
    private List<Integer> tags;
    public RecycleViewItemderoctor(Context context, List<Integer> tags) {
        this.context = context;
        this.mPaint = new Paint();
        this.tags = tags;
    }

    @Override
    public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {
        super.onDraw(c, parent, state);
    }

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    @Override
    public void onDrawOver(Canvas c, RecyclerView parent, RecyclerView.State state) {
        super.onDrawOver(c, parent, state);
        int childCount = parent.getChildCount();
        int left = parent.getPaddingLeft();
        int rigt = parent.getWidth()-parent.getPaddingRight();
        int tag = -1;
        int preTag;
        for (int i = 0; i < childCount; i++) {
            View child = parent.getChildAt(i);
            if (null == child) {
                continue;
            }
            int index = parent.getChildAdapterPosition(child);
            int top = child.getTop();
            int bottom = child.getBottom();
            int width = parent.getWidth() / 2;
            preTag = tag;
            tag = tags.get(index);
            if (tag == preTag) {
                continue;
            }
            //悬停头部
            int height = Math.max(top, mHeigh);
            //当后个头部到达时，当前头部逐渐消失 达到顶替的效果
            if (index + 1 < tags.size()) {
                if (tags.get(index + 1) != tag) {
                    height = Math.min(bottom, mHeigh);
                }
            }
            mPaint.setColor(Color.YELLOW);
            Rect rect = new Rect(left, height - mHeigh, rigt, height);
            c.drawRect(rect, mPaint);
            mPaint.setColor(Color.GREEN);
            mPaint.setTextSize(24);
            c.drawText("分组头" + tags.get(index), width, height - mHeigh / 2, mPaint);
        }
    }

    @Override
    public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
        super.getItemOffsets(outRect, view, parent, state);
        int position = parent.getChildAdapterPosition(view);
        int preTag = -1;
        if (position - 1 > 0) {
            preTag = tags.get(position - 1);
        }
        if (tags.get(position) != preTag) {
            outRect.top = mHeigh;
        }
    }
}
